package com.jinke.api.config;

import com.plexpt.chatgpt.ChatGPT;
import com.plexpt.chatgpt.util.Proxys;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.Proxy;

@Configuration
@ConditionalOnProperty(name = "gpt.key")
public class GptConfig {
    @Value("${gpt.key}")
    private String key;

    @Value("${gpt.host}")
    private String host;

    @Bean("chatGPT")
    public ChatGPT chatGPT() {

        if ("true".equals(System.getenv("local"))) {

            Proxy proxy = Proxys.http("127.0.0.1", 7890);
            ChatGPT chart = ChatGPT.builder()
                    .apiKey(key)
                    .proxy(proxy)
                    .timeout(900)
                    .apiHost(host) //反向代理地址
                    .build()
                    .init();

            return chart;
        } else {
            ChatGPT chart = ChatGPT.builder()
                    .apiKey(key)
                    .timeout(900)
                    .apiHost(host) //反向代理地址
                    .build()
                    .init();
            return chart;
        }

    }

}
